﻿using System.Collections.Generic;
using System.Linq;
using AMM.Model;

namespace AMM.DataAccess {
    public class ReportRepository : BaseRepository<User> {

        public List<MaterialStoreView> GetMaterialStoreViewList(
            string materialCode
            , string storeBasisCode
            , decimal? quantityStart
            , decimal? quantityEnd) {
            using (var ctx = this.Context) {
                var query = ctx.MaterialStoreViews.Where(x => 1 == 1);

                if (!string.IsNullOrEmpty(materialCode)) {
                    query = query.Where(x => x.MaterialCode.Contains(materialCode));
                }
                if (!string.IsNullOrEmpty(storeBasisCode)) {
                    query = query.Where(x => x.StoreBasisCode.Contains(storeBasisCode));
                }
                if (quantityStart.HasValue) {
                    query = query.Where(x => x.Quantity >= quantityStart.Value);
                }
                if (quantityEnd.HasValue) {
                    query = query.Where(x => x.Quantity <= quantityEnd.Value);
                }

                return query.ToList();
            }
        }

        public List<ProductStoreView> GetProductStoreViewList(
            string productCode
            , string storeBasisCode
            , decimal? quantityStart
            , decimal? quantityEnd) {
            using (var ctx = this.Context) {
                var query = ctx.ProductStoreViews.Where(x => 1 == 1);

                if (!string.IsNullOrEmpty(productCode)) {
                    query = query.Where(x => x.ProductCode.Contains(productCode));
                }
                if (!string.IsNullOrEmpty(storeBasisCode)) {
                    query = query.Where(x => x.StoreBasisCode.Contains(storeBasisCode));
                }
                if (quantityStart.HasValue) {
                    query = query.Where(x => x.Quantity >= quantityStart.Value);
                }
                if (quantityEnd.HasValue) {
                    query = query.Where(x => x.Quantity <= quantityEnd.Value);
                }

                return query.ToList();
            }
        }

        public List<RequirementAnalysis_Result> GetRequirementAnalysisProcedureList(
            int conditionType
            , string materialCode
            , string produceConducts) {
            using (var ctx = this.Context) {
                return ctx.RequirementAnalysis(conditionType, materialCode, produceConducts).ToList();
            }
        }

        public List<SuspenseUnQualifiedView> GetSuspenseUnQualifiedViewList(
            string materialCode
            , int? vendorId) {
            using (var ctx = this.Context) {
                var query = ctx.SuspenseUnQualifiedViews.Where(x => 1 == 1);

                if (!string.IsNullOrEmpty(materialCode)) {
                    query = query.Where(x => x.MaterialCode.Contains(materialCode));
                }
                if (vendorId.HasValue) {
                    query = query.Where(x => x.VendorId == vendorId.Value);
                }

                return query.ToList();
            }
        }

        public List<BOMCostView> GetBOMCostViewList(string bomCode, string orderBy) {
            using (var ctx = this.Context) {
                var query = ctx.BOMCostViews.Where(x => x.OrderBy == orderBy);

                if (!string.IsNullOrEmpty(bomCode)) {
                    query = query.Where(x => x.ProductCode == bomCode);
                }

                return query.ToList();
            }
        }

        public List<ProduceConductCostView> GetProduceConductCostViewList(string produceConductCode) {
            using (var ctx = this.Context) {
                var query = ctx.ProduceConductCostViews.Where(x => x.ProduceConductCode == produceConductCode);

                return query.ToList();
            }
        }
    }
}
